#include <iostream>
#include <vector>
#include <unistd.h>
#include "thread.hpp"
#include "mutex.hpp"
using namespace std;
using namespace ThreadModual;
using namespace MutexModel;
#define NUM 4
Mutex mutex;

int ticket = 10000;
void run()
{

    while (true)
    {
        LockGuard l(mutex);
        if (ticket > 0)
        {
            
            usleep(1000);
            cout << ticket-- << endl;
        }
        else
        {
            break;
        }
    }
}

int main()
{
    std::vector<Thread> threads;
    for (int i = 0; i < NUM; i++)
    {
        threads.emplace_back(run);
    }
    for (auto &t : threads)
    {
        t.Start();
    }
    for (auto &t : threads)
    {
        t.Join();
    }
    return 0;
}